/**
 * 用户列表 add by gsh
 */
$(function() {
	// toolbar添加
	$("#btnAdd").click(function() {
		var index = msgWnd.open({
			title : "添加用户信息",
			skin : 'layui-layer-lan',
			type : 2,
			shade : 0.5,
			area : [ "530px", "550px" ],
			maxmin : true,
			content : '/user/add',
			zIndex : layer.zIndex
		});
	});

	// toolbar修改
	$("#btnEdit").click(function() {
		var checked = getChecked();
		if (checked.length != 1) {
			msgTools.alert("错误", "请选择1条要操作的记录！", 500);
			return;
		}
		edit(checked[0]);
	});

	// 全选
	$("#chkAll").change(function() {
		var chkStatus = $(this).is(":checked");
		$("[id^=chk]:not(#chkAll)").each(function() {
			this.checked = chkStatus;
		});
	});

	// toolbar批量禁用
	$("#btnDisable").click(function() {
		var checked = getChecked();
		if (checked.length == 0) {
			msgTools.alert("错误", "请选择要操作的记录！", 500);
			return;
		}
		$.ajax({
			type : "POST",
			url : "/user/disable/batch",
			data : JSON.stringify(checked),
			contentType : "application/json",
			dataType : "json",
			success : function(msg) {
				if (msg.data.status == 1) {
					msgTools.alert("批量禁用", "禁用成功。", 1000);
					if(typeof(reloadPg)!="undefined")
						reloadPg();
					else
						window.location.href = window.location.href;
				} else {
					msgTools.alert("批量禁用", "禁用失败。", 2000);
				}
			}
		});
	});

	// toolbar批量启用
	$("#btnEnable").click(function() {
		var checked = getChecked();
		if (checked.length == 0) {
			msgTools.alert("错误", "请选择要操作的记录", 500);
			return;
		}
		$.ajax({
			type : "POST",
			url : "/user/enable/batch",
			data : JSON.stringify(checked),
			contentType : "application/json",
			dataType : "json",
			success : function(msg) {
				if (msg.data.status == 1) {
					msgTools.alert("批量启用", "启用成功。", 1000);
					if(typeof(reloadPg)!="undefined")
						reloadPg();
					else
						window.location.href = window.location.href;
				} else {
					msgTools.alert("批量启用", "启用失败。", 2000);
				}
			}
		});
	});
});

function getChecked() {
	var map = $("[id^=chk]:not(#chkAll)").map(function() {
		if (this.checked)
			return this.value;
	});
	return map.get();
}

// 禁用启用
function enableUser(_id, _isEnable) {
	$.ajax({
		type : "POST",
		url : "/user/enable",
		data : "id=" + _id + "&isEnable=" + _isEnable,
		dataType : "json",
		success : function(msg) {
			if (msg.data.status == 1) {
				msgTools.alert("修改用户状态", "修改成功。", 1000);
				if(typeof(reloadPg)!="undefined")
					reloadPg();
				else
					window.location.href = window.location.href;
			} else {
				msgTools.alert("修改用户状态", "修改失败。", 2000);
			}
		}
	});
}

// 编辑
function edit(id) {
	var index = msgWnd.open({
		title : "修改用户信息",
		skin : 'layui-layer-lan',
		type : 2,
		shade : 0.5,
		area : [ "510px", "460px" ],
		maxmin : true,
		content : '/user/edit/' + id,
		zIndex : layer.zIndex
	});
}

// 查看
function view(id) {
	var index = msgWnd.open({
		title : "查看用户信息",
		skin : 'layui-layer-lan',
		type : 2,
		shade : 0.5,
		area : [ "510px", "560px" ],
		maxmin : true,
		content : '/user/view/' + id,
		zIndex : layer.zIndex
	});	
}

// 删除
function deleteUser(_id) {
	var index = msgWnd.confirm("确认删除吗?", {
		title : "删除",
		skin : 'layui-layer-lan',
		btn : [ "确定", "取消" ],
		icon : 1
	}, function() {
		$.ajax({
			type : "POST",
			url : "/user/delete/" + _id,
			dataType : "json",
			success : function(msg) {
				if (msg.data.status == 1) {
					msgTools.alert("删除用户", "删除成功。", 1000);
					if(typeof(reloadPg)!="undefined")
						reloadPg();
					else
						window.location.href = window.location.href;
				} else {
					msgTools.alert("删除用户", "删除失败。", 2000);
				}
			}
		});		
		msgWnd.close(index);
	});
}

function selDateRange() {
	var index = msgWnd.open({
		title : "选择查询日期",
		skin : 'layui-layer-lan',
		type : 2,
		shade : 0.5,
		area : [ "510px", "220px" ],
		maxmin : true,
		content : '/views/pages/ddl/ddlDateRange.jsp',
		zIndex : layer.zIndex
	});	
	msgWnd.selectResultCallBack=function(val){
		$("#exportForm").find("[name=createTime]").val(val.time1);
		$("#exportForm").find("[name=createTime1]").val(val.time2);
		$('#exportForm').submit();
	};	
}

//客服
function feedBack(mobile) {
	var index = msgWnd.open({
		title : "查看客服记录",
		skin : 'layui-layer-lan',
		type : 2,
		shade : 0.5,
		area : [ "90%", "90%" ],
		maxmin : true,
		content : '/feedback/serviceRecordList/' + mobile,
		zIndex : layer.zIndex
	});	
}
